package spindles.gwt.client;


import spindles.gwt.shared.Const;

import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.DockPanel;
import com.google.gwt.user.client.ui.FileUpload;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

public class ImportPanel extends RightPanel implements ProgressHandler{
	
	private Boolean showAllExecutions = new Boolean(false);
	
	private ProgressBarHelper progressHelper = new ProgressBarHelper(this, "Importing EEG data...");

	public ImportPanel() {
		setSplitBottomWidget();	
		progressHelper.checkProgress();
		init(createToolbar(), splitTopWidget, splitBottomWidget);
		progressHelper.initPollTimer();
	}
	
	private void setSplitBottomWidget(){		
		AsyncCallback c = setDetailsTable(Const.AUDIT_TABLENAME, 
				Const.AUDIT_FIELDS);
		if(showAllExecutions.booleanValue()){
			ServerGateway.getAudits(c);			
		} else{
			ServerGateway.getRecentAudits(100, c);
		}
		
		replaceSplitBottomWidget(detailsTable);				
	}	
	
	private Toolbar createToolbar(){
		Toolbar toolbar = new Toolbar(Const.IMPORT_AUDIT_TITLE);
		
		CheckBox criterionCB = new CheckBox("Show all executions");
		criterionCB.setChecked(false);
		criterionCB.setStyleName("spindles-CheckBox");
		
		criterionCB.addClickListener(new ClickListener() {
	      public void onClick(Widget sender) {
	        boolean showAll = ((CheckBox) sender).isChecked();
	        showAllExecutions = Boolean.valueOf(showAll);
	        setSplitBottomWidget();      
	        
	      }
	    });	
		
		toolbar.add(criterionCB);
		return toolbar;
	}
	
	private Panel createWidgets(){
		DockPanel mainPanel = new DockPanel();
		
		VerticalPanel panel = new VerticalPanel();
		
		Label pathInstr = new Label("Enter the path of the file with the EEG data you want to import.");	
		Label examplePath = new Label("e.g., C:\\eeg\\eeg_20080115.dat");
		
		
		final FileUpload upload = new FileUpload();		
		DOM.setElementProperty(upload.getElement(), "size", "70");
		
		final CheckBox duplPersonCB = new CheckBox("Allow import of persons with duplicate name");
		duplPersonCB.setChecked(false);		
		
		Label channel = new Label("Enter the name of the EEG channel to import.");
		Label exChannel = new Label("e.g., Cz");
		final TextBox channelTB = new TextBox();
		channelTB.setMaxLength(10);
		channelTB.setVisibleLength(10);
		
		Button impButton = new Button("Import EEG data");
		impButton.addClickListener(new ClickListener() {
			public void onClick(Widget sender) {
				((Button)sender).setEnabled(false);
				Callback command = new Callback() {
					public void execute(Object result) {								
						progressHelper.checkProgress();
						progressHelper.initPollTimer();
					}			
				};				
				
				ServerGateway.importSessionPartData(upload.getFilename(), 
						channelTB.getText(), 
						duplPersonCB.isChecked(), 
						Spindles.createCallBack(command));
				progressHelper.pollServer(1000);
			}
		});			
		
		panel.setSpacing(4);
		
		panel.add(new Label(""));
		panel.add(pathInstr);		
		panel.add(examplePath);
		panel.add(new Label(""));
		panel.add(upload);		
		panel.add(new Label(""));
		
		panel.add(channel);
		panel.add(exChannel);
		panel.add(new Label(""));
		panel.add(channelTB);
		panel.add(new Label(""));
		
		panel.add(duplPersonCB);
		panel.add(new Label(""));
		
		panel.add(impButton);
		
		panel.setWidth("500px");		
		
		SimplePanel filler = new SimplePanel();
		filler.setWidth("150px");
		mainPanel.add(filler, DockPanel.WEST);
		mainPanel.add(panel, DockPanel.CENTER);	
		mainPanel.setWidth("100%");
		
		mainPanel.setSpacing(4);		
				
		return mainPanel;
	}

	public void update(Object o) {}

	public void hideProgressBar() {
		replaceSplitTopWidget(createWidgets());
		setSplitBottomWidget();		
	}

	public void showProgressBar(Panel progressbar) {
		replaceSplitTopWidget(progressbar);		
	}

}
